package com.cqyanyu.yanyu.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.widget.TextView;

import com.cqyanyu.yanyu.R;


/**
 * Created by suju on 14-5-12.
 */
public class FontIconView extends TextView {

    private String fontFileName;
    private Context mContext;
    private String text;
    private String icon_Top;
    private String icon_Bottom;
    private String icon_Right;
    private String icon_Left;

    public FontIconView(Context context) {
        super(context);
        mContext = context;
        initView();

    }

    public FontIconView(Context context, android.util.AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.icontextview);
        icon_Top = ta.getString(R.styleable.icontextview_icon_Top);
        icon_Left = ta.getString(R.styleable.icontextview_icon_Left);
        icon_Bottom = ta.getString(R.styleable.icontextview_icon_Bottom);
        icon_Right = ta.getString(R.styleable.icontextview_icon_Right);
        fontFileName = ta.getString(R.styleable.icontextview_FontFileName);
        Mosaic();
        initView();
    }

    private void Mosaic() {
        text = getText().toString();
        text = parseEmpty(icon_Left) + text + parseEmpty(icon_Right);
        if (!parseEmpty(icon_Top).equals("")) {
            text = parseEmpty(icon_Top) + "\n" + text;
        }
        if (!parseEmpty(icon_Bottom).equals("")) {
            text = text + "\n" + parseEmpty(icon_Bottom);
        }
        setText(text);
    }

    public void setIconText(String text) {
        setText(text);
        this.text = text;
        Mosaic();
    }

    public void setIcon_Top(String icon_Top) {
        this.icon_Top = icon_Top;
        Mosaic();
    }

    public void setIcon_Right(String icon_Right) {
        this.icon_Right = icon_Right;
        Mosaic();
    }

    public void setIcon_Left(String icon_Left) {
        this.icon_Left = icon_Left;
        Mosaic();
    }

    public void setIcon_Bottom(String icon_Bottom) {
        this.icon_Bottom = icon_Bottom;
        Mosaic();
    }


    private String parseEmpty(String str) {
        if (str == null || "null".equals(str.trim())) {
            str = "";
        }
        return str.trim();
    }

    private void initView() {
        if (TextUtils.isEmpty(fontFileName)) {
            fontFileName = "iconfont.ttf";
        }
        Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), fontFileName);
        setTypeface(iconfont);
    }
}  